package com.srit.system.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.srit.core.bean.Page;
import com.srit.forecast.bean.ReportConfig;
import com.srit.forecast.dao.ReportConfigDao;
import com.srit.system.dao.PageConfigDao;
import com.srit.system.service.ReportConfigService;

@Controller
public class ReportConfigController {
	@Autowired
	private ReportConfigDao reportConfigDao;

	@Autowired
	private PageConfigDao pageConfigDao;

	@Autowired
	private ReportConfigService reportConfigService;

	@RequestMapping(value = "/reportConfig-search")
	public String search(@ModelAttribute Page page) {
		reportConfigDao.page(page);
		return "config/reportConfig";
	}

	@RequestMapping(value = "/reportConfig-form")
	public String form(@ModelAttribute ReportConfig reportConfig) {
		return "config/reportConfig";
	}

	@RequestMapping(value = "/reportConfig-edit")
	public String edit(@ModelAttribute ReportConfig reportConfig,
			@RequestParam int id, ModelMap modelMap) {
		BeanUtils.copyProperties(reportConfigService.getById(id), reportConfig);
		modelMap.put("pageConfigList", pageConfigDao.listAll());
		return "config/reportConfig";
	}

	@RequestMapping(value = "/reportConfig-saveOrUpdate")
	public String saveOrUpdate(ReportConfig reportConfig) {
		if (reportConfig.getId() == null || reportConfig.getId() == 0) {
			reportConfigService.save(reportConfig);
		} else {
			reportConfigService.update(reportConfig);
		}
		return "redirect:/reportConfig-search";
	}

}
